﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LightSwitch;
namespace LightSwitchApplication
{
    public partial class SalesOrder
    {

        partial void OrderCount_Compute(ref short result)
        {
            result = (short)(this.SalesOrderItems !=null ? this.SalesOrderItems.Count(): 0);
        }

        partial void OrderQty_Compute(ref decimal result)
        {
            if (this.SalesOrderItems != null)
            {
                result = (from items in this.SalesOrderItems
                          select (decimal)(items.ItemQty != null ? items.ItemQty : 0)).Sum();
            }
            else result = 0;
        }

        partial void OrderDiscount_Compute(ref decimal result)
        {
            if (this.OrderShop.ShopCustomer != null && this.OrderShop.ShopCustomer.CustomerDiscountPercent != null)
            {
                result = (decimal)(this.OrderShop.ShopCustomer.CustomerDiscountPercent) * (decimal)0.01;
            }
            else
            {
                result = 0;
            }
        }

        partial void OrderAmount_Compute(ref decimal result)
        {
            result = (from items in this.SalesOrderItems
                      select (decimal)items.ItemSum).Sum() - OrderDiscount;
        }

        partial void OrderCurrentState_Compute(ref string result)
        {
            if (this.DocumentStatusSet != null && this.DocumentStatusSet.Count() > 0)
            {
                result = this.DocumentStatusSet.OrderBy(sos => sos.StatusDate).Last().DocumentState.Name;
            }
         }
    }
}
